home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / System / XADmaster / xad_dev / Sources / clients / xadIO.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  5.8 KB  |  240 lines

  1. #ifndef XADMASTER_IO_C
  2. #define XADMASTER_IO_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        xadIO.c
  7.     Main:        xadmaster
  8.     Versionstring:    $VER: xadIO.c 1.2 (24.03.2001)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    input/output functions
  12.  
  13.  1.0   24.10.00 : first version, based on older seperate files
  14.  1.1   07.11.00 : moved xadIOPutChar, xadIOGetChar into structure to
  15.     support different functions
  16.  1.2   24.03.01 : added ReadBits versions
  17. */
  18.  
  19. /* In case you are calling this file directly from source, you may use this
  20. defines to make the functions static, inlined or whatever else. In case it
  21. should be used a sobject file, they must be cleared, as they default to
  22. static. */
  23.  
  24. /* the main functions */
  25. #ifndef XADIOFUNCMODE
  26. #define XADIOFUNCMODE static
  27. #endif
  28.  
  29. /* the bit functions */
  30. #ifndef XADIOFUNCMODEBITS
  31. #define XADIOFUNCMODEBITS XADIOFUNCMODE
  32. #endif
  33.  
  34. #define XADIODIRECTMODE
  35. #include "xadIO.h"
  36. #include <exec/memory.h>
  37.  
  38. #define XIDBUFSIZE        10240
  39.  
  40. static UBYTE xadIOPutFunc(struct xadInOut *io, UBYTE data)
  41. {
  42.   if(!io->xio_Error)
  43.   {
  44.     if(!io->xio_OutSize && !(io->xio_Flags & XADIOF_NOOUTENDERR))
  45.     {
  46.       io->xio_Error = XADERR_DECRUNCH;
  47.       io->xio_Flags |= XADIOF_ERROR;
  48.     }
  49.     else
  50.     {
  51.       if(io->xio_OutBufferPos >= io->xio_OutBufferSize)
  52.         xadIOWriteBuf(io);
  53.       io->xio_OutBuffer[io->xio_OutBufferPos++] = data;
  54.       if(!--io->xio_OutSize)
  55.         io->xio_Flags |= XADIOF_LASTOUTBYTE;
  56.     }
  57.   }
  58.   return data;
  59. }
  60.  
  61. static UBYTE xadIOGetFunc(struct xadInOut *io)
  62. {
  63.   UBYTE res = 0;
  64.  
  65.   if(!io->xio_Error)
  66.   {
  67.     if(!io->xio_InSize)
  68.     {
  69.       if(!(io->xio_Flags & XADIOF_NOINENDERR))
  70.       {
  71.         io->xio_Error = XADERR_DECRUNCH;
  72.         io->xio_Flags |= XADIOF_ERROR;
  73.       }
  74.     }
  75.     else
  76.     {
  77.       if(io->xio_InBufferPos >= io->xio_InBufferSize)
  78.       {
  79.         ULONG i;
  80.         struct xadMasterBase *xadMasterBase = io->xio_xadMasterBase;
  81.  
  82.         if((i = io->xio_InBufferSize) > io->xio_InSize)
  83.           i = io->xio_InSize;
  84.         if(!io->xio_ArchiveInfo)
  85.         {
  86.           io->xio_Flags |= XADIOF_ERROR;
  87.           io->xio_Error = XADERR_DECRUNCH;
  88.         }
  89.         else if(!(io->xio_Error = xadHookTagAccess(XADAC_READ, i, io->xio_InBuffer, io->xio_ArchiveInfo,
  90.         XAD_USESKIPINFO, 1, TAG_DONE)))
  91.         {
  92.           if(io->xio_InFunc)
  93.             io->xio_InFunc(io, i);
  94.           res = *io->xio_InBuffer;
  95.         }
  96.         else
  97.           io->xio_Flags |= XADIOF_ERROR;
  98.         io->xio_InBufferPos = 1;
  99.       }
  100.       else
  101.         res = io->xio_InBuffer[io->xio_InBufferPos++];
  102.       --io->xio_InSize;
  103.     }
  104.     if(!io->xio_InSize)
  105.       io->xio_Flags |= XADIOF_LASTINBYTE;
  106.   }
  107.  
  108.   return res;
  109. }
  110.  
  111. XADIOFUNCMODE struct xadInOut *xadIOAlloc(ULONG flags, struct xadArchiveInfo *ai, struct xadMasterBase *xadMasterBase)
  112. {
  113.   ULONG size = sizeof(struct xadInOut);
  114.   struct xadInOut *io;
  115.  
  116.   if(flags & XADIOF_ALLOCINBUFFER)
  117.     size += XIDBUFSIZE;
  118.   if(flags & XADIOF_ALLOCOUTBUFFER)
  119.     size += XIDBUFSIZE;
  120.   if((io = (struct xadInOut *) xadAllocVec(size, MEMF_CLEAR|MEMF_PUBLIC)))
  121.   {
  122.     STRPTR b;
  123.  
  124.     b = (STRPTR) (io+1);
  125.     io->xio_Flags = flags;
  126.     io->xio_PutFunc = xadIOPutFunc;
  127.     io->xio_GetFunc = xadIOGetFunc;
  128.     io->xio_ArchiveInfo = ai;
  129.     io->xio_xadMasterBase = xadMasterBase;
  130.     if(flags & XADIOF_ALLOCINBUFFER)
  131.     {
  132.       io->xio_InBuffer = b; b += XIDBUFSIZE;
  133.       io->xio_InBufferSize = io->xio_InBufferPos = XIDBUFSIZE;
  134.     }
  135.     if(flags & XADIOF_ALLOCOUTBUFFER)
  136.     {
  137.       io->xio_OutBuffer = b;
  138.       io->xio_OutBufferSize = XIDBUFSIZE;
  139.     }
  140.   }
  141.   return io;
  142. }
  143.  
  144. #ifdef XADIOGETBITSLOW
  145. XADIOFUNCMODEBITS ULONG xadIOGetBitsLow(struct xadInOut *io, UBYTE bits)
  146. {
  147.   ULONG x;
  148.  
  149.   while(io->xio_BitNum < bits)
  150.   {
  151.     io->xio_BitBuf |= xadIOGetChar(io) << io->xio_BitNum;
  152.     io->xio_BitNum += 8;
  153.   }
  154.   x = io->xio_BitBuf & ((1<<bits)-1);
  155.   io->xio_BitBuf >>= bits;
  156.   io->xio_BitNum -= bits;
  157.   return x;
  158. }
  159. #endif
  160.  
  161. #ifdef XADIOREADBITSLOW
  162. XADIOFUNCMODEBITS ULONG xadIOReadBitsLow(struct xadInOut *io, UBYTE bits)
  163. {
  164.   while(io->xio_BitNum < bits)
  165.   {
  166.     io->xio_BitBuf |= xadIOGetChar(io) << io->xio_BitNum;
  167.     io->xio_BitNum += 8;
  168.   }
  169.   return io->xio_BitBuf & ((1<<bits)-1);
  170. }
  171.  
  172. XADIOFUNCMODEBITS void xadIODropBitsLow(struct xadInOut *io, UBYTE bits)
  173. {
  174.   io->xio_BitBuf >>= bits;
  175.   io->xio_BitNum -= bits;
  176. }
  177. #endif
  178.  
  179. #ifdef XADIOGETBITSHIGH
  180. XADIOFUNCMODEBITS ULONG xadIOGetBitsHigh(struct xadInOut *io, UBYTE bits)
  181. {
  182.   ULONG x;
  183.  
  184.   while(io->xio_BitNum < bits)
  185.   {
  186.     io->xio_BitBuf = (io->xio_BitBuf << 8) | xadIOGetChar(io);
  187.     io->xio_BitNum += 8;
  188.   }
  189.   x = (io->xio_BitBuf >> (io->xio_BitNum-bits)) & ((1<<bits)-1);
  190.   io->xio_BitNum -= bits;
  191.   return x;
  192. }
  193. #endif
  194.  
  195. #ifdef XADIOREADBITSHIGH
  196. XADIOFUNCMODEBITS ULONG xadIOReadBitsHigh(struct xadInOut *io, UBYTE bits)
  197. {
  198.   while(io->xio_BitNum < bits)
  199.   {
  200.     io->xio_BitBuf = (io->xio_BitBuf << 8) | xadIOGetChar(io);
  201.     io->xio_BitNum += 8;
  202.   }
  203.   return (io->xio_BitBuf >> (io->xio_BitNum-bits)) & ((1<<bits)-1);
  204. }
  205.  
  206. XADIOFUNCMODEBITS void xadIODropBitsHigh(struct xadInOut *io, UBYTE bits)
  207. {
  208.   io->xio_BitNum -= bits;
  209. }
  210. #endif
  211.  
  212. XADIOFUNCMODE LONG xadIOWriteBuf(struct xadInOut *io)
  213. {
  214.   if(!io->xio_Error && io->xio_OutBufferPos)
  215.   {
  216.     struct xadMasterBase *xadMasterBase = io->xio_xadMasterBase;
  217.  
  218.     if(io->xio_OutFunc)
  219.       io->xio_OutFunc(io, io->xio_OutBufferPos);
  220.     if(!(io->xio_Flags & XADIOF_COMPLETEOUTFUNC))
  221.     {
  222.       if(!io->xio_ArchiveInfo)
  223.       {
  224.         io->xio_Flags |= XADIOF_ERROR;
  225.         io->xio_Error = XADERR_DECRUNCH;
  226.       }
  227.       else if((io->xio_Error = xadHookTagAccess(XADAC_WRITE, io->xio_OutBufferPos,
  228.       io->xio_OutBuffer, io->xio_ArchiveInfo,
  229.       io->xio_Flags & XADIOF_NOCRC16 ? TAG_IGNORE : XAD_GETCRC16, &io->xio_CRC16,
  230.       io->xio_Flags & XADIOF_NOCRC32 ? TAG_DONE   : XAD_GETCRC32, &io->xio_CRC32,
  231.       TAG_DONE)))
  232.         io->xio_Flags |= XADIOF_ERROR;
  233.     }
  234.     io->xio_OutBufferPos = 0;
  235.   }
  236.   return io->xio_Error;
  237. }
  238.  
  239. #endif /* XADMASTER_IO_C */
  240.